﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace HuaXingSDK
{
    public static class Exts
    {
        public static string GetPostMessage(this XmlDocument xml, bool isAsyn = true,
            HXPriority priority = HXPriority.普通)
        {
            var outerXml = xml.GetOuterXml();
            var sign = Uilts.GetSign(outerXml);
            var msg = "001X" + (int) priority + (isAsyn ? "2" : "1").PadRight(HXConfigs.MsgHeadLength);
            msg += sign.Length.ToString().PadLeft(HXConfigs.MsgSecLength, '0');
            msg += sign;
            msg += outerXml;
            return msg;
        }

        public static string GetOuterXml(this XmlDocument xml)
        {
            var outerXml = string.Empty;
            foreach (XmlNode node in xml.ChildNodes)
            {
                var desc = node as XmlDeclaration;
                if (desc != null)
                {
                    outerXml += string.Format("<?xml version=\"{0}\" encoding=\"{1}\"?>",
                        desc.Version, desc.Encoding);
                }
                else
                {
                    outerXml += GetOuterXml(node);
                }
            }
            return outerXml;
        }

        private static string GetOuterXml(XmlNode node)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("<{0}>", node.Name);
            if (node.HasChildNodes)
            {
                foreach (XmlNode sub in node.ChildNodes)
                {
                    sb.Append(sub.NodeType == XmlNodeType.Text ? node.InnerText : GetOuterXml(sub));
                }
            }
            else
            {
                sb.Append(node.InnerText);
            }
            sb.AppendFormat("</{0}>", node.Name);
            return sb.ToString();
        }

        internal static string GetResponseXml(this HXRequest request, HXPriority priority = HXPriority.普通)
        {
            var postmsg = request.ToEncryptXml().GetPostMessage(false, priority);
            var resmsg = HXConfigs.CreatePoster(request).Post(HXConfigs.请求地址, postmsg);
            string xml;
            if (Uilts.CheckSign(resmsg, out xml) == false)
                throw new CheckSignException(resmsg);
            return xml;
        }

        internal static XmlNode[] ToArray(this XmlNodeList nodeList)
        {
            var list = new List<XmlNode>();
            foreach (XmlNode item in nodeList)
            {
                list.Add(item);
            }
            return list.ToArray();
        }

        internal static XmlNode GetNodeByName(this XmlNode node, string name)
        {
            if (node == null)
                return null;

            foreach (XmlNode sub in node.ChildNodes)
            {
                if (sub.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                    return sub;
            }
            return null;
        }

        internal static bool OICEquals(this string str1, string str2)
        {
            return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
        }
    }
}
